﻿''' <summary>
''' The main XMLmosaic functions such as System.Console, variables and String concatenation.
''' </summary>
''' <remarks></remarks>
Module Basic

    ''' <summary>
    ''' The list of variables that have been made.
    ''' </summary>
    ''' <remarks></remarks>
    Public varlist As String = ""

    ''' <summary>
    ''' The output of th latest string concatenation.
    ''' </summary>
    ''' <remarks></remarks>
    Public Strs As String = ""

    ''' <summary>
    ''' Processes the Console.WriteLine() command.</summary>
    ''' <param name="Member">The memeber of the System.Console namespace.</param>
    ''' <remarks>Only processes Console.Writeline().</remarks>
    Sub ConsoleP(ByVal Member As String)

        Dim words As String = Member
        Dim split As String() = words.Split(New [Char]() {"("c, ")"c})

        If split(0) = "WriteLine" Then

            Dim args As String = split(1)

            Console.WriteLine(Statements.StmtsGetVal(args))

            'If args Like "* + *" = False Then
            '    If args.Contains("'") = True Then
            '        args = args.Trim("'"c)
            '        Console.Write(args)
            '    ElseIf args.Contains("'") = False Then

            '        If GetVal(args) <> "" Then
            '            args = GetVal(args)
            '        End If
            '        Console.Write(args)
            '    End If
            '    Console.Write(vbLf)
            'ElseIf args Like "* + *" Then
            '    Dim argsplit As String() = args.Split(New [Char]() {"+"c})
            '    Dim arg As String

            '    For Each arg In argsplit
            '        If arg.Contains("'") = True Then
            '            arg = arg.Trim()
            '            arg = arg.Trim("'"c)
            '            Console.Write(arg)
            '        ElseIf arg.Contains("'") = False Then
            '            arg = arg.Trim()
            '            If GetVal(arg) <> "" Then
            '                arg = GetVal(arg)
            '            End If
            '            Console.Write(arg)
            '        End If

            '    Next
            '    Console.Write(vbLf)
            'End If

        End If
    End Sub

    ''' <summary>
    ''' Creates a new variable.
    ''' </summary>
    ''' <param name="Var">The variable declaration for the variable to create.</param>
    ''' <remarks>Accepts input in the form type name</remarks>
    Sub NewVar(ByVal Var As String)
        Dim words As String = Var
        Dim split As String() = words.Split(New [Char]() {" "c})

        Dim declaredvars As String() = varlist.Split(New [Char]() {";"c})
        Dim Exists As Boolean = False

        Dim s As String
        For Each s In declaredvars
            If s = split(1) Then
                Exists = True
            End If
        Next

        If Exists = False Then
            varlist = varlist & split(1) & ";"

            If split(0) = "string" Then
                AddVar(split(1), "string")
            ElseIf split(0) = "int" Then
                AddVar(split(1), "integer")
            ElseIf split(0) = "double" Then
                AddVar(split(1), "double")
            ElseIf split(0) = "bool" Then
                AddVar(split(1), "boolean")
            ElseIf split(0) = "void" Then
                AddVar(split(1), "string")
            End If
        End If
    End Sub

    ''' <summary>
    ''' Adds a new variable to the variable file.
    ''' </summary>
    ''' <param name="Name">The name of the variable to create.</param>
    ''' <param name="Type">The type of the variable to create.</param>
    ''' <remarks></remarks>
    Sub AddVar(ByVal Name As String, ByVal Type As String)
        Dim Vars As XElement = XElement.Load(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\vars.xml")
        Dim Count As XElement = <root>
                                    <%= From el In Vars.<Counter>.<Count> _
                                        Select el %>
                                </root>
        Dim Countt As Integer = Count

        Dim xml As String = ""
        xml = xml & "<Variables>" & vbCr & "<Type>XMLmosaic Variables</Type>" & vbCr
        Dim id As Integer = 0

        Do Until id = Countt

            id = id + 1

            Dim Namel As XElement = <root>
                                        <%= From el In Vars.<Var>.<Name> _
                                            Where el.Attribute("id") = id.ToString _
                                            Select el %>
                                    </root>
            Dim Namet As String = Namel

            Dim Typel As XElement = <root>
                                        <%= From el In Vars.<Var>.<Type> _
                                            Where el.Attribute("id") = id.ToString _
                                            Select el %>
                                    </root>
            Dim Typet As String = Typel

            Dim Valuel As XElement = <root>
                                         <%= From el In Vars.<Var>.<Value> _
                                             Where el.Attribute("id") = id.ToString _
                                             Select el %>
                                     </root>
            Dim Valuet As String = Valuel

            Dim Var As XElement = _
                              <Var>
                                  <Name id=<%= id %>><%= Namet %></Name>
                                  <Type id=<%= id %>><%= Typet %></Type>
                                  <Value id=<%= id %>><%= Valuet %></Value>
                              </Var>

            xml = xml & Var.ToString & vbCr
        Loop

        id = id + 1
        Dim def As String = " "
        Dim NewVar As XElement = _
                               <Var>
                                   <Name id=<%= id %>><%= Name %></Name>
                                   <Type id=<%= id %>><%= Type %></Type>
                                   <Value id=<%= id %>><%= def %></Value>
                               </Var>


        Dim Counts As XElement = _
                                 <Counter>
                                     <Count><%= id %></Count>
                                 </Counter>

        xml = xml & NewVar.ToString & vbCr & Counts.ToString & vbCr & "</Variables>"


        My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\vars.xml", xml, False)

    End Sub

    ''' <summary>
    ''' Assigns a value to a variable.
    ''' </summary>
    ''' <param name="assignment">The assignment expression as entered by the programmer.</param>
    ''' <remarks></remarks>
    Sub AssignVal(ByVal assignment As String)

        Dim readwords As String = assignment
        Dim readsplit() As String = {"", ""}

        Dim i As Integer = -1
        Dim len As Integer = readwords.Length - 1
        Dim arr As String = ""
        Dim ch As String = ""
        Dim ins As Boolean = False

        Do Until i = len
            i = i + 1
            ch = readwords.Chars(i)

            If ch = "'" Then
                If ins = False Then
                    ins = True
                ElseIf ins = True Then
                    ins = False
                End If
            End If

            If ch = "=" And ins = True Then
                arr = arr & ch
            ElseIf ch = "=" And ins = False Then
                arr = arr.Trim()
                readsplit(0) = arr
                arr = ""
            Else
                arr = arr & ch
            End If

            If i = len Then
                arr = arr.Trim()
                readsplit(1) = arr
                arr = ""
            End If
        Loop


        readsplit(0) = readsplit(0).Trim()
        readsplit(1) = readsplit(1).Trim()

        Dim Typer As String = GetTyp(readsplit(0))

        If readsplit(1) = "Console.ReadLine()" Then
            If Typer = "string" Then
                Dim value As String = Console.ReadLine()
                assignment = readsplit(0) & " = " & "'" & value & "'"
            ElseIf Typer = "boolean" Then
                Dim value As String = Console.ReadLine()
                assignment = readsplit(0) & " = " & value
            ElseIf Typer = "integer" Or Typer = "double" Then
                Dim value As String = Console.ReadLine()
                assignment = readsplit(0) & " = " & value
            End If
        ElseIf readsplit(1) Like "FileSystem.ReadAllText(*)" Then
            Dim bsplit As String() = readsplit(1).Split(New [Char]() {"("c, ")"c})
            Dim address As String = bsplit(1).Trim()
            address = Statements.StmtsGetVal(address)
            Dim value As String = My.Computer.FileSystem.ReadAllText(address)

            If Typer = "string" Then
                assignment = readsplit(0) & " = " & "'" & value & "'"
            ElseIf Typer = "boolean" Then
                assignment = readsplit(0) & " = " & value
            ElseIf Typer = "integer" Or Typer = "double" Then
                assignment = readsplit(0) & " = " & value
            End If
        ElseIf readsplit(1).Contains(" * ") = True Or readsplit(1).Contains(" / ") = True _
        Or readsplit(1).Contains(" - ") = True Or readsplit(1).Contains(" + ") = True _
        Or readsplit(1).Contains("Sqrt|") = True And readsplit(1).Contains("'") = False _
        Or readsplit(1).Contains("Pi|") = True Or readsplit(1) Like "*.*(*)*" Or readsplit(1) Like "*.*.*(*)*"  And readsplit(1) <> "Console.ReadLine()" And readsplit(1).Contains("FileSystem.ReadAllText") = False Then

            'If readsplit(1) <> "Console.ReadLine()" And readsplit(1).Contains("FileSystem.ReadAllText") = False And readsplit(1) Like "*.*(*)*" Or readsplit(1) Like "*.*.*(*)*" Then

            '    EvalStr(readsplit(1))
            '    Executor.Process("string str")
            '    Executor.Process("str = " & "'" & Strs & "'")
            '    assignment = readsplit(0) & " = " & "str"

            'End If

            If GetTyp(readsplit(0)) = "integer" Or GetTyp(readsplit(0)) = "double" Then
                Math.MathExpr(readsplit(1))
                assignment = readsplit(0) & " = " & Math.Ans
            End If

            If GetTyp(readsplit(0)) = "string" Then
                EvalStr(readsplit(1))
                assignment = readsplit(0) & " = " & "'" & Strs & "'"
            End If

            
            End If


        Dim prewords As String = assignment
        Dim presplit() As String = {"", ""}

        Dim i2 As Integer = -1
        Dim len2 As Integer = prewords.Length - 1
        Dim arr2 As String = ""
        Dim ch2 As String = ""
        Dim ins2 As Boolean = False

        Do Until i2 = len2
            i2 = i2 + 1
            ch2 = prewords.Chars(i2)

            If ch2 = "'" Then
                If ins2 = False Then
                    ins2 = True
                ElseIf ins2 = True Then
                    ins2 = False
                End If
            End If

            If ch2 = "=" And ins2 = True Then
                arr2 = arr2 & ch2
            ElseIf ch2 = "=" And ins2 = False Then
                arr2 = arr2.Trim()
                presplit(0) = arr2
                arr2 = ""
            Else
                arr2 = arr2 & ch2
            End If

            If i2 = len2 Then
                arr2 = arr2.Trim()
                presplit(1) = arr2
                arr2 = ""
            End If
        Loop

            presplit(0) = presplit(0).Trim(" ")
            presplit(1) = presplit(1).Trim(" ")

            Dim Type As String = GetTyp(presplit(0))

            If Type = "string" Then
                If presplit(1) Like "*'*'*" Or presplit(1) Like "'*'" Then
                Else
                    assignment = presplit(0) & " = " & GetVal(presplit(1))
                End If
            ElseIf Type = "boolean" Then
                If presplit(1) Like "true" Or presplit(1) Like "false" Or presplit(1) Like "False" Or presplit(1) Like "True" Then
                Else
                    assignment = presplit(0) & " = " & GetVal(presplit(1))
                End If
            ElseIf Type = "integer" Or Type = "double" Then
                If presplit(1) Like "[A-Z]*" Or presplit(1) Like "[a-z]*" Or presplit(1) Like "[A-Z]" Or presplit(1) Like "[a-z]" Then
                    assignment = presplit(0) & " = " & GetVal(presplit(1))
                End If
            End If

            Dim Vars As XElement = XElement.Load(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\vars.xml")
            Dim Count As XElement = <root>
                                        <%= From el In Vars.<Counter>.<Count> _
                                            Select el %>
                                    </root>
            Dim Countt As Integer = Count


            Dim words As String = assignment
            Dim split As String() = words.Split(New [Char]() {"="c})

            split(0) = split(0).Trim(" ")
            split(1) = split(1).Trim(" ")


            Dim xml As String = ""
            xml = xml & "<Variables>" & vbCr & "<Type>XMLmosaic Variables</Type>" & vbCr
            Dim id As Integer = 0

            Dim NameSearch As XElement = <root>
                                             <%= From el In Vars.<Var>.<Name> _
                                                 Where el.Value = split(0) _
                                                 Select el.Attribute("id").Value %>
                                         </root>
            Dim elid As Integer = NameSearch

            Do Until id = elid - 1

                id = id + 1

                Dim Namel As XElement = <root>
                                            <%= From el In Vars.<Var>.<Name> _
                                                Where el.Attribute("id") = id.ToString _
                                                Select el %>
                                        </root>
                Dim Namet As String = Namel

                Dim Typel As XElement = <root>
                                            <%= From el In Vars.<Var>.<Type> _
                                                Where el.Attribute("id") = id.ToString _
                                                Select el %>
                                        </root>
                Dim Typet As String = Typel

                Dim Valuel As XElement = <root>
                                             <%= From el In Vars.<Var>.<Value> _
                                                 Where el.Attribute("id") = id.ToString _
                                                 Select el %>
                                         </root>
                Dim Valuet As String = Valuel

                Dim Var As XElement = _
                                  <Var>
                                      <Name id=<%= id %>><%= Namet %></Name>
                                      <Type id=<%= id %>><%= Typet %></Type>
                                      <Value id=<%= id %>><%= Valuet %></Value>
                                  </Var>

                xml = xml & Var.ToString & vbCr
            Loop

            id = id + 1

            Dim Namec As XElement = <root>
                                        <%= From el In Vars.<Var>.<Name> _
                                            Where el.Attribute("id") = id.ToString _
                                            Select el %>
                                    </root>
            Dim Nametc As String = Namec

            Dim Typec As XElement = <root>
                                        <%= From el In Vars.<Var>.<Type> _
                                            Where el.Attribute("id") = id.ToString _
                                            Select el %>
                                    </root>
            Dim Typetc As String = Typec

            Dim CurVar As XElement

            If Typetc = "string" Then
                split(1) = split(1).Trim("'"c)
                Dim val As String = CStr(split(1))
                Dim element As XElement = _
                <Var>
                    <Name id=<%= id %>><%= Nametc %></Name>
                    <Type id=<%= id %>><%= Typetc %></Type>
                    <Value id=<%= id %>><%= val %></Value>
                </Var>
                CurVar = element
            ElseIf Typetc = "integer" Then
                Dim val As Integer = CInt(split(1))
                Dim element As XElement = _
             <Var>
                 <Name id=<%= id %>><%= Nametc %></Name>
                 <Type id=<%= id %>><%= Typetc %></Type>
                 <Value id=<%= id %>><%= val %></Value>
             </Var>
                CurVar = element
            ElseIf Typetc = "double" Then
                Dim val As Double = CDbl(split(1))
                Dim element As XElement = _
             <Var>
                 <Name id=<%= id %>><%= Nametc %></Name>
                 <Type id=<%= id %>><%= Typetc %></Type>
                 <Value id=<%= id %>><%= val %></Value>
             </Var>
                CurVar = element
            ElseIf Typetc = "boolean" Then
                Dim val As Boolean = CBool(split(1))
                Dim element As XElement = _
             <Var>
                 <Name id=<%= id %>><%= Nametc %></Name>
                 <Type id=<%= id %>><%= Typetc %></Type>
                 <Value id=<%= id %>><%= val %></Value>
             </Var>
                CurVar = element
            Else
                Dim element As XElement = _
             <Var>
                 <Name id=<%= id %>><%= Nametc %></Name>
                 <Type id=<%= id %>><%= Typetc %></Type>
                 <Value id=<%= id %>></Value>
             </Var>
                CurVar = element

            End If


            xml = xml & CurVar.ToString & vbCr


            Do Until id = Countt

                id = id + 1

                Dim Namel As XElement = <root>
                                            <%= From el In Vars.<Var>.<Name> _
                                                Where el.Attribute("id") = id.ToString _
                                                Select el %>
                                        </root>
                Dim Namet As String = Namel

                Dim Typel As XElement = <root>
                                            <%= From el In Vars.<Var>.<Type> _
                                                Where el.Attribute("id") = id.ToString _
                                                Select el %>
                                        </root>
                Dim Typet As String = Typel

                Dim Valuel As XElement = <root>
                                             <%= From el In Vars.<Var>.<Value> _
                                                 Where el.Attribute("id") = id.ToString _
                                                 Select el %>
                                         </root>
                Dim Valuet As String = Valuel

                Dim Var As XElement = _
                                  <Var>
                                      <Name id=<%= id %>><%= Namet %></Name>
                                      <Type id=<%= id %>><%= Typet %></Type>
                                      <Value id=<%= id %>><%= Valuet %></Value>
                                  </Var>

                xml = xml & Var.ToString & vbCr
            Loop

            Dim Counts As XElement = _
                                     <Counter>
                                         <Count><%= id %></Count>
                                     </Counter>

            xml = xml & Counts.ToString & vbCr & "</Variables>"


            My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\vars.xml", xml, False)

    End Sub

    ''' <summary>
    ''' Gets the value of a particular variable.
    ''' </summary>
    ''' <param name="VarName">The name of the variable to get the value of.</param>
    ''' <returns>The variables's value.</returns>
    ''' <remarks></remarks>
    Function GetVal(ByVal VarName As String)
        Dim Vars As XElement = XElement.Load(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\vars.xml")
        Dim Val As String

        Dim NameSearch As XElement = <root>
                                         <%= From el In Vars.<Var>.<Name> _
                                             Where el.Value = VarName _
                                             Select el.Attribute("id").Value %>
                                     </root>
        Dim elid As Integer = NameSearch

        Dim ValSearch As XElement = <root>
                                        <%= From el In Vars.<Var>.<Value> _
                                            Where el.Attribute("id") = elid.ToString _
                                            Select el %>
                                    </root>
        Val = ValSearch

        Return Val
    End Function

    ''' <summary>
    ''' Gets the type of a particular variable.
    ''' </summary>
    ''' <param name="VarName">The name of the variable to get the type of.</param>
    ''' <returns>The variable's type.</returns>
    ''' <remarks></remarks>
    Function GetTyp(ByVal VarName As String)
        Dim Vars As XElement = XElement.Load(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\vars.xml")
        Dim Typ As String

        Dim NameSearch As XElement = <root>
                                         <%= From el In Vars.<Var>.<Name> _
                                             Where el.Value = VarName _
                                             Select el.Attribute("id").Value %>
                                     </root>
        Dim elid As Integer = NameSearch

        Dim TypSearch As XElement = <root>
                                        <%= From el In Vars.<Var>.<Type> _
                                            Where el.Attribute("id") = elid.ToString _
                                            Select el %>
                                    </root>
        Typ = TypSearch

        Return Typ
    End Function


    ''' <summary>
    ''' Evaluates a string conacatenation.
    ''' </summary>
    ''' <param name="expr">The expression containing the string concatenation.</param>
    ''' <remarks></remarks>
    Sub EvalStr(ByVal expr As String)

        Dim output As String = ""

        expr = expr.Trim()
        Dim arr As String = ""
        Dim len As Integer = expr.Length - 1

        Dim ins As Boolean = False

        Dim ch As String = ""
        Dim i As Integer = -1

        Do Until i = len
            i = i + 1
            ch = expr.Chars(i)

            If ch = "'" Then
                If ins = False Then
                    ins = True
                ElseIf ins = True Then
                    ins = False
                End If
            End If

            If ins = False Then
                If ch <> "+" Then
                    arr = arr & ch
                End If
            ElseIf ins = True Then
                arr = arr & ch
            End If

            If ch = "+" And ins = False Then
                arr = arr.Trim()

                If arr Like "*.*(*)" Or arr Like "*(*)" Then
                    arr = arr.Trim()

                    Relations.MakeVar = True
                    Relations.Input(arr)
                    Relations.MakeVar = False

                    arr = GetVal(Relations.CurrentMethod)

                    output = output & arr
                ElseIf arr Like "'*'" = True Then
                    arr = arr.Trim()
                    arr = arr.Trim("'")
                    output = output & arr
                ElseIf arr Like "'*'" = False Then
                    arr = arr.Trim()
                    If GetVal(arr) <> "" Then
                        arr = GetVal(arr)
                    End If
                    output = output & arr
                End If
                arr = ""
            End If

            If i = len Then
                arr = arr.Trim()

                If arr Like "*.*(*)" Or arr Like "*(*)" Then
                    arr = arr.Trim()

                    Relations.MakeVar = True
                    Relations.Input(arr)
                    Relations.MakeVar = False

                    arr = GetVal(Relations.CurrentMethod)

                    output = output & arr
                ElseIf arr Like "'*'" = True Then
                    arr = arr.Trim()
                    arr = arr.Trim("'")
                    output = output & arr
                ElseIf arr Like "'*'" = False Then
                    arr = arr.Trim()
                    If GetVal(arr) <> "" Then
                        arr = GetVal(arr)
                    End If
                    output = output & arr
                End If
                arr = ""
            End If


        Loop


        'For Each arg In argsplit
        '    If arg.Contains("'") = True Then
        '        arg = arg.Trim()
        '        arg = arg.Trim("'")
        '        output = output & arg
        '    ElseIf arg.Contains("'") = False Then
        '        arg = arg.Trim()
        '        If GetVal(arg) <> "" Then
        '            arg = GetVal(arg)
        '        End If
        '        output = output & arg
        '    End If

        'Next

        Strs = output
    End Sub

End Module
